<template>
  <a-modal
    :visible="visible"
    :title="t('edit')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel(false)"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
    
      <a-form-item ref="login_name" :label="t('login name')" name="login_name">
        <a-input has-feedback v-model:value="formState.login_name" />
      </a-form-item> 

       <a-form-item ref="resetpass" :label="t('reset password')" name="resetpass">
     
        <a-switch :checkedValue="true" :unCheckedValue="false" :checkedChildren="t('open')" :unCheckedChildren="t('close')" v-model:checked="formState.resetpass" />
     
      </a-form-item> 

      <a-form-item v-if="formState.resetpass" has-feedback  ref="password" :label="t('password')" name="password" >
        <a-input v-model:value="formState.password" type="password"/>
      </a-form-item>
       <a-form-item v-if="formState.resetpass" has-feedback ref="repeatpassword" :label="t('repeat password')" name="repeatpassword">
        <a-input v-model:value="formState.repeatpassword" type="password"/>
      </a-form-item>
      <a-form-item  ref="user_name" :label="t('user name')" name="user_name">
        <a-input v-model:value="formState.user_name" />
      </a-form-item>
       <a-form-item has-feedback ref="phone" :label="t('phone')" name="phone">
        <a-input v-model:value="formState.phone" />
      </a-form-item>
      <a-form-item has-feedback ref="email" :label="t('email')" name="email">
        <a-input v-model:value="formState.email" />
      </a-form-item>   
      <a-form-item :label="t('status')" name="status">
        <a-switch :checkedValue="1" :unCheckedValue="0" :checkedChildren="t('open')" :unCheckedChildren="t('close')" v-model:checked="formState.status" />
     
      </a-form-item>
      

 
    </a-form>
  </a-modal>
</template>
<script>
import { useI18n  } from "vue-i18n";
import { message } from 'ant-design-vue';
import { edit } from "@/core/api/module/user";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object
    }
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [],
      showComponent: true,
      showPath: true,
      showCode: false
    });
    
    const formRef = ref(

    );
    
    let formState = reactive({ 
      password:''
    });

     watch(props,(props) => { 
      formState.id = props.record.id;
      formState.login_name = props.record.login_name;
      formState.user_name = props.record.user_name;
      formState.status = props.record.status;
      formState.phone = props.record.phone;
      formState.email = props.record.email; 
    })

    let validatePass = async (_rule, value) => {
      if (value === '') {
        return Promise.reject('Please input the password');
      } else {
        if (formState.password !== '') {
          formRef.value.validateFields('repeatpassword');
        }
        return Promise.resolve();
      }
    };
    let validatePass2 = async (_rule, value) => {
      if (value === '') {
        return Promise.reject(t('Please input the password again'));
      } else if (value !== formState.password) {
        return Promise.reject(t("Two inputs don't match!"));
      } else {
        return Promise.resolve();
      }
    };
  
     const formRules = {
      username: [{ required: true , message: t('Please input user name'), trigger: "blur" }],
      password: [{ required: true, validator:validatePass,  trigger: "blur" }],  
      repeatpassword: [{ required: true, validator:validatePass2, trigger: "blur" }], 
      phone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message:t('Please input the correct mobile phone number'), trigger: "blur" }], 
      email: [{ required: true, pattern:/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,message:t('Please input the correct email format'), trigger: "blur" }], 
    }; 

    const submit = (e) => {  
      
      console.log(formRef.value);
      formRef.value
        .validate()
        .then(() => {              
          edit(toRaw(formState)).then((response)=>{
              if(response.code == 0){
                message.success({ content: t('Saved successfully!'), duration: 1 }).then(()=>{
                  cancel(true);
                });
              }else{
                message.error({ content: response.msg, duration: 1 }).then(()=>{
                  cancel(false);
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

     
    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };
   

    return {
      t,
      state,
      submit, 
      cancel,
      formRef,
      formState,
      formRules,
      
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
    };
  },
});
</script>